package com.gqb.aop;

import java.lang.reflect.Method;
import java.util.List;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;


/**
 * 用来创建代理类的
 * @author 小泽玛丽罗
 *
 */
public class ProxyManager {
	private List<Proxy> proxyList;
	private Class<?> targetClass;
	public ProxyManager(List<Proxy> proxyList, Class<?> targetClass) {
		super();
		this.proxyList = proxyList;
		this.targetClass = targetClass;
	}
	
	@SuppressWarnings("unchecked")
	public <T> T createProxy(){
		Enhancer enhancer=new Enhancer();
		enhancer.setSuperclass(targetClass);
		enhancer.setCallback(new MethodInterceptor() {
			
			public Object intercept(Object targetObject, Method method, Object[] args,
					MethodProxy methodProxy) throws Throwable {
				
				return new ProxyChain(targetClass, proxyList, method, methodProxy, targetObject, args).doProxyChain();
			}
		});
		return  (T)enhancer.create();
	}
}
